package com.atguigu.gmall.pms.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuMapper;
import com.atguigu.gmall.pms.vo.SaleAttrValueVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.service.SkuAttrValueService;


@Service("skuAttrValueService")
public class SkuAttrValueServiceImpl extends ServiceImpl<SkuAttrValueMapper, SkuAttrValueEntity> implements SkuAttrValueService {

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Autowired
    private SkuMapper skuMapper;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<SkuAttrValueEntity> querySkuAttrValuesByCidAndskuId(Long cid, Long skuId) {

        //根据分类id查询销售属性id
        List<AttrEntity> attrEntities = attrMapper.selectList(new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getCategoryId, cid)
                .eq(AttrEntity::getSearchType, 1).eq(AttrEntity::getType, 0));
        if (CollectionUtils.isEmpty(attrEntities)){
            //将对象转为id集合
            return null;
        }
        List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
        //根据attrId和skuId查询属性值
        List<SkuAttrValueEntity> skuAttrValueEntities = this.list(new LambdaQueryWrapper<SkuAttrValueEntity>().eq(SkuAttrValueEntity::getSkuId, skuId)
                .in(SkuAttrValueEntity::getAttrId, attrIds));
        return skuAttrValueEntities;
    }

    @Override
    public List<SaleAttrValueVo> querySaleAttrValuesBySupId(Long spuId) {
        //根据supId查询sku
        List<SkuEntity> skuEntities = skuMapper.selectList(new LambdaQueryWrapper<SkuEntity>().eq(SkuEntity::getSpuId, spuId));
        if (CollectionUtils.isEmpty(skuEntities)){
            return null;
        }

        //将sku实体类对象转为skuIds集合
        List<Long> skuIds = skuEntities.stream().map(SkuEntity::getId).collect(Collectors.toList());

        //根据skuIds查询销售属性信息
        List<SkuAttrValueEntity> skuAttrValueEntities = this.list(new LambdaQueryWrapper<SkuAttrValueEntity>().in(SkuAttrValueEntity::getSkuId, skuIds));
        if (CollectionUtils.isEmpty(skuAttrValueEntities)){
            return null;
        }

        //将 skuAttrValueEntities 转换为vo集合
        //[{attrId:3,attrName:机身颜色,:attrValues:[黑色，金色,白色]},
        // {attrId:4,attrName:运行内存,attrValues:[6G,8G,12G]},
        // {attrId:5,attrName:机身内存,attrValues:[128G,256G,512G]}]
        //分组完成之后 以attrId 为key 以这组数据为value
        Map<Long, List<SkuAttrValueEntity>> map = skuAttrValueEntities.stream().collect(Collectors.groupingBy(SkuAttrValueEntity::getAttrId));
        List<SaleAttrValueVo> saleAttrValueVos = new ArrayList<>();
        //遍历map 将每一个map转换为一个vo对象
        map.forEach(((attrId, AttrValueEntities) -> {
            SaleAttrValueVo saleAttrValueVo = new SaleAttrValueVo();
            saleAttrValueVo.setAttrId(attrId);
            //有该分组的情况下 至少有一个分组
            saleAttrValueVo.setAttrName(AttrValueEntities.get(0).getAttrName());
            //把该组集合 转化为规格参数值的set集合
            saleAttrValueVo.setAttrValues(AttrValueEntities.stream().map(SkuAttrValueEntity::getAttrValue).collect(Collectors.toSet()));
            saleAttrValueVos.add(saleAttrValueVo);
        }));

        return saleAttrValueVos;
    }

    @Override
    public String queryMappingBySpuId(Long spuId) {
        //根据spuId查询sku信息
        List<SkuEntity> skuEntities = skuMapper.selectList(new LambdaQueryWrapper<SkuEntity>().eq(SkuEntity::getSpuId, spuId));
        if (CollectionUtils.isEmpty(skuEntities)){
            return null;
        }

        //将sku转换为skuIds
        List<Long> skuIds = skuEntities.stream().map(SkuEntity::getId).collect(Collectors.toList());
        //根据skuIds查询 attr_values与sku_id的映射关系
        List<Map<String,Object>> maps = skuAttrValueMapper.queryMappingBySkuIds(skuIds);
        //转换为json字符串
        if (CollectionUtils.isEmpty(maps)){
            return null;
        }
        Map<String, Long> mappingMap = maps.stream().collect(Collectors.toMap(map -> map.get("attr_values").toString(), map -> (Long) map.get("sku_id")));
        return JSON.toJSONString(mappingMap);
    }

}